package com.bookmg.controller;

import com.bookm.service.MkyService;
import com.bookmg.entity.Book;
import com.bookmg.entity.Btype;
import com.bookmg.entity.UsersMky;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MkyController {
    @Autowired
    private MkyService mkyService;
    @RequestMapping("mkytest")
    @ResponseBody
    public List<UsersMky> getAllUserCat(){
        return mkyService.getAllUserCar();
    }

    @RequestMapping("testBook2")
    @ResponseBody
    public List<Book> getAllBook(){
        return mkyService.getAllBook();
    }
    @RequestMapping("getBtype")
    @ResponseBody
    public List<Btype> getAllBtype(){
        return mkyService.getAllBtype();
    }

    @RequestMapping("mkyhomeView")
    public String getAllUserCatView(Model model){
        List<UsersMky> allUserCar = mkyService.getAllUserCar();
        List<Btype> allBtype = mkyService.getAllBtype();
        List<Book> recBook = mkyService.recommendBook();
        System.out.println(allUserCar.size() + " " + recBook.size());
        model.addAttribute("userCars",allUserCar);
        model.addAttribute("Btype",allBtype);
        model.addAttribute("RecBook",recBook);
        return "WEB-INF/jsp/mky/home.jsp";
    }


    @RequestMapping("/testBook3")
    public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum
            ,@RequestParam(defaultValue = "1",value = "pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Book> list = mkyService.getAllBook();
        PageInfo<Book> pageInfo = new PageInfo<Book>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "WEB-INF/jsp/mky/test.jsp";
    }
}
